home *** CD-ROM | disk | FTP | other *** search
/ Super PC 31 / Super PC 31 (Shareware).iso / spc / inter / speakf / fuente / utility.c < prev   
Encoding:
C/C++ Source or Header  |  1995-10-05  |  4.8 KB  |  198 lines

  1. /*
  2.  
  3.             Utility functions
  4.             
  5. */
  6.  
  7. #include "netfone.h"
  8.  
  9. //  RSTRING  --  Retrieve a string from the resource file.
  10.  
  11. char *rstring(int resid)
  12. {
  13. #define maxCStrings 10              // Maximum concurrently used strings
  14.     static char rstrings[maxCStrings][80];
  15.     static int n = 0;
  16.     int m = n;
  17.  
  18. #ifdef COUNT_RSTRING
  19.     static DWORD callCount = 0;
  20.     static int reCurso = 0;
  21.     
  22.     callCount++;
  23.     if (!reCurso) {
  24.         reCurso = TRUE;
  25.         propeller(IDC_PH_PACKET_SIZE, callCount);
  26.         reCurso = FALSE;
  27.     }
  28. #endif
  29.     if (LoadString(hInst, resid, rstrings[m], 79) < 0) {
  30.         strcpy(rstrings[m], "");
  31.     }
  32.     n = (n + 1) % maxCStrings;
  33.     return rstrings[m];
  34. }
  35.  
  36. /*    RFILTER  --  Load a filter from the resource file and convert
  37.                  vertical bars to zero characters.  */
  38.                  
  39. char *rfilter(int resid)
  40. {
  41.     char *cp, *cp1;
  42.     
  43.     cp1 = cp = rstring(resid);
  44.     while (*cp1 != 0) {
  45.         if (*cp1 == '|') {
  46.             *cp1 = 0;
  47.         }
  48.         cp1++;
  49.     }
  50.     return cp;
  51. }                
  52.  
  53. //  MSGBOX  --  Format a message box with wsprintf and display
  54.  
  55. INT MsgBox(HWND hwndParent, UINT fuType, LPSTR pszFormat, ...)
  56. {
  57.     CHAR szOutput[MAX_PRINTF_OUTPUT];
  58.     va_list ArgList;
  59.  
  60.     va_start(ArgList, pszFormat);
  61.     wvsprintf(szOutput, pszFormat, ArgList);
  62.     va_end(ArgList);
  63.  
  64.     return MessageBox(hwndParent, szOutput, pszAppName, fuType);
  65. }
  66.  
  67. /*  WINPRINTF  --  Format a message with wsprintf and display
  68.                    in a window with TextOut.  */
  69.  
  70. VOID WinPrintf(HDC hdc, INT row, INT col, LPSTR pszFormat, ...)
  71. {
  72.     CHAR szOutput[MAX_PRINTF_OUTPUT];
  73.     INT cbOutput;
  74.     va_list ArgList;
  75.  
  76.     va_start(ArgList, pszFormat);
  77.     cbOutput = wvsprintf(szOutput, pszFormat, ArgList);
  78.     va_end(ArgList);
  79.  
  80.     TextOut(hdc, col * tmAveCharWidth, row * tmHeight,
  81.              szOutput, cbOutput);
  82. }
  83.  
  84. /*  SOCKERRTOSTRING  --  Return text corresponding to error codes
  85.                          from WINSOCK.  */
  86.  
  87. LPSTR SockerrToString(SOCKERR serr)
  88. {
  89.     //    It would be too easy if these error codes were contiguous
  90.     static SOCKERR socketErrorCodes[] = {
  91.         WSAENAMETOOLONG, WSANOTINITIALISED, WSASYSNOTREADY, WSAVERNOTSUPPORTED,
  92.         WSAESHUTDOWN, WSAEINTR, WSAHOST_NOT_FOUND, WSATRY_AGAIN, WSANO_RECOVERY,
  93.         WSANO_DATA, WSAEBADF, WSAEWOULDBLOCK, WSAEINPROGRESS, WSAEALREADY,
  94.         WSAEFAULT, WSAEDESTADDRREQ, WSAEMSGSIZE, WSAEPFNOSUPPORT, WSAENOTEMPTY,
  95.         WSAEPROCLIM, WSAEUSERS, WSAEDQUOT, WSAESTALE, WSAEINVAL, WSAEMFILE,
  96.         WSAEACCES, WSAELOOP, WSAEREMOTE, WSAENOTSOCK, WSAEADDRNOTAVAIL,
  97.         WSAEADDRINUSE, WSAEAFNOSUPPORT, WSAESOCKTNOSUPPORT, WSAEPROTONOSUPPORT,
  98.         WSAENOBUFS, WSAETIMEDOUT, WSAEISCONN, WSAENOTCONN, WSAENOPROTOOPT,
  99.         WSAECONNRESET, WSAECONNABORTED, WSAENETDOWN, WSAENETRESET, WSAECONNREFUSED,
  100.         WSAEHOSTDOWN, WSAEHOSTUNREACH, WSAEPROTOTYPE, WSAEOPNOTSUPP,
  101.         WSAENETUNREACH, WSAETOOMANYREFS
  102.     };
  103.     int i;
  104.  
  105.     for (i = 0; i < ELEMENTS(socketErrorCodes); i++) {
  106.         if (serr == socketErrorCodes[i]) {  
  107.             return rstring(IDS_SOCKET_ERRORS + i);
  108.         }
  109.     }
  110.     return rstring(IDS_SOCKET_ERROR_UNKNOWN);    
  111. }
  112.  
  113. //    RESETSOCKET  --  Perform a hard close on a socket
  114.  
  115. SOCKERR ResetSocket(SOCKET sock)
  116. {
  117.     LINGER linger;
  118.  
  119.     if (sock == INVALID_SOCKET) {
  120.         return 0;
  121.     }
  122.  
  123.     /*  Enable linger with a timeout of zero.  This will
  124.         force the hard close when we call closesocket().
  125.  
  126.          We ignore the error return from setsockopt.  If it
  127.         fails, we'll just try to close the socket anyway. */
  128.  
  129.     linger.l_onoff  = TRUE;
  130.     linger.l_linger = 0;
  131.  
  132.     setsockopt(sock, SOL_SOCKET, SO_LINGER, (CHAR FAR *) &linger, sizeof(linger));
  133.  
  134.     return closesocket(sock);
  135. }
  136.  
  137. //    CREATESOCKET  --  Create a new socket
  138.  
  139. SOCKERR CreateSocket(SOCKET FAR * psock, INT type, ULONG address, PORT port)
  140. {
  141.     SOCKET  sNew;
  142.     SOCKERR serr;
  143.  
  144.     //  Create the socket
  145.  
  146.     sNew = socket(PF_INET, type, 0);
  147.     serr = (sNew == INVALID_SOCKET) ? WSAGetLastError(): 0;
  148.  
  149.     if ((serr == 0)
  150.             && (alwaysBindSocket || (port != 0))
  151.         ) {
  152.         SOCKADDR_IN sockAddr;
  153.  
  154.         //  Bind an address to the socket
  155.  
  156.         sockAddr.sin_family = AF_INET;
  157.         sockAddr.sin_addr.s_addr = address;
  158.         sockAddr.sin_port = port;
  159.  
  160.         if (bind(sNew, (SOCKADDR FAR *) &sockAddr, sizeof(sockAddr)) != 0) {
  161.             serr = WSAGetLastError();
  162.         }
  163.     }
  164.  
  165.     if (serr != 0) {
  166.         ResetSocket(sNew);
  167.         sNew = INVALID_SOCKET;
  168.     }
  169.  
  170.     *psock = sNew;
  171.     return serr;
  172. }
  173.  
  174. //  REVSHORT  --  Reverse bytes in a short
  175.  
  176. void revshort(short FAR *s)
  177. {
  178.     short s1 = *s;
  179.     LPSTR ip = (LPSTR) &s1, op = (LPSTR) s;
  180.     
  181.     op[0] = ip[1];
  182.     op[1] = ip[0];
  183. }
  184.  
  185. //  REVLONG  --  Reverse bytes in a long
  186.  
  187. void revlong(long FAR *l)
  188. {
  189.     long l1 = *l;
  190.     LPSTR ip = (LPSTR) &l1, op = (LPSTR) l;
  191.     
  192.     op[0] = ip[3];
  193.     op[1] = ip[2];
  194.     op[2] = ip[1];
  195.     op[3] = ip[0]; 
  196. }
  197.  
  198.